﻿function SetupControls() {
    $("[propertyname]").change(function() { UpdateField(this, "Documento"); });
    $(".date").datepicker();

    if (action != "search") {
        if (action == "create") {
            AddValidators();
            SetValidation("Documento");
            $("[propertyname]").removeAttr("disabled");
            $("#Documento_CodDocumento").attr("disabled", "disabled");
            $("#btnBuscar").hide();
        }
        if (action == "edit") {
            AddValidators();
            SetValidation("Documento");
            $("[propertyname]").removeAttr("disabled");
            $("#Documento_CodDocumento").attr("disabled", "disabled");
            $("#btnBuscar").hide();
        }
        if (action == "query") {
            $("[propertyname]").attr("disabled", "disabled");
            $("#Documento_CodDocumento").removeAttr("disabled");
            $("#btnBuscar").show();
            $("#btnBuscar").click(function() {
                DoSearch();
            });
        }
    }
    else {
        $("[propertyname]").attr("disabled", "disabled");
        $("#Documento_CodDocumento").removeAttr("disabled");
        $("#btnBuscar").show();
        $("#btnBuscar").click(function() {
            DoSearch();
        });
    }
}

function SetupActions() {
    $("#btnNuevo").click(function() {
        window.location = "/documento/create";
    });

    $("#btnEditar").click(function() {
    var cod = $('#Documento_CodDocumento').val();
        window.location = "/documento/edit?cod=" + cod;
    });

    $("#btnGrabar").click(function() {
        if (action == "create" || action == "edit")
            Save();
    });

    $("#btnCancelar").click(function() {
        window.location = "/documento/search";
    });
}

function DoSearch() {
    var cod = $('#Documento_CodDocumento').val();
    window.location = "/documento/doSearch?cod=" + cod;
}

function Save() {
    if ($('img.validation:visible').length) {
        alert('Please correct all validation errors before saving.');
        return;
    }

    var data = { id: guid, action: action };
    $.ajax({
        type: "POST",
        url: GetControllerUrl("Save", "Documento"),
        data: data,
        success: function(result) {
            alert(result.message);
            setTimeout(window.location = "/documento/search", 2000);
        }
    });
}